<template>
	<view  class="login-form">
		
			<tm-images :width="200" :height="200" :round="4" src="../../static/logo.png"></tm-images>
			<tm-row>
				<view class="text-blue flex-center text-weight-b mt-20">用心做软件</view>
			</tm-row>
			<tm-sheet color="blue" :shadow="24">
				<view class="text-size-s text-weight-b mb-24">MiniTool</view>
				<tm-sheet :margin="[0,0]" :padding="[0,0]" classname="overflow" :shadow="24">
					<tm-input  :required="true" title="账号" v-model="login_form.user_name" placeholder="请输入账号" align="right"></tm-input>
					<tm-input  :required="true" title="密码" v-model="login_form.passsword" placeholder="请输入密码" align="right"></tm-input>
					<tm-row v-if="captchaBase64">
						<tm-col :grid="6">
							<tm-images :src="captchaBase64" :previmage="false" model="widthFix" @click="createCaptcha()"></tm-images>
						</tm-col>
						<tm-col :grid="6">
							<tm-input v-model="login_form.captcha"></tm-input>
						</tm-col>
					</tm-row>
				</tm-sheet>
			</tm-sheet>
		
		<tm-row class="pt-n10">
			<tm-col :grid="6">
				<tm-button @click="login()">登录</tm-button>
			</tm-col>
			<tm-col :grid="6">
				<tm-button @click="RegisterPage()">注册</tm-button>
			</tm-col>
		</tm-row>
		<!-- <tm-sticky model="bottom">
			<tm-sheet >版权所有©本心</tm-sheet>
			</tm-sticky> -->
	</view>

</template>

<script>
	import upApp from '../../uni_modules/uni-upgrade-center-app/utils/check-update.js'
	
	export default {
		data() {
			return {
				login_form:{
					user_name:'admin',
					passsword:'Bdd901129',
					captcha:'',
					auto_login:true
				},
				needCaptcha:false,
				captchaBase64:""
			}
		},
		onLoad() {
			this.check_update()
		},
		methods: {
			check_update() {
				upApp()
			},
			//创建验证码
			createCaptcha(){
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'createCaptcha',
						params:{
							scene: "login"
						},
					},
					success: (suc) => {
						
						this.captchaBase64 = suc.result.captchaBase64
						console.log(this.captchaBase64)
					}
					
				})
			},
			//跳转到注册页面
			RegisterPage(){
				uni.navigateTo({
					url: './register',
					animationType:'slide-in-bottom',
					animationDuration:500,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			//登录选择-根据宿主环境选择不同的登录策略
			login() {
				
				uni.setStorageSync('login_form',this.login_form)
				
				// #ifdef MP-WEIXIN
				this.WXlogin()
				// #endif
				// #ifndef MP-WEIXIN
				this.Applogin()
				// #endif
				
			},
			/* APP登录逻辑 */
			Applogin() {
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'login',
						params:{
							"username":this.login_form.user_name,
							"password":this.login_form.passsword,
							"captcha":this.login_form.captcha
						}
					},
					success: (suc) => {
						console.log(suc)
						if(suc.result.code === 0){
							uni.navigateTo({
								url:'../index/index'
							})
						}
						if(suc.result.needCaptcha){
							this.needCaptcha = true
							this.createCaptcha()
						}
						
					}
				})
			},
			/* 微信小程序登录逻辑 */
			WXlogin() {

			},
			/* 网页端登录逻辑，暂时不用 */
			H5login() {

			}
		}
	}
</script>

<style>
	.login-form{
		position: absolute;
		
		top: 5%;
		left: 0;
		right: 0;
	}
</style>
